﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Remoting.Controller;
using Pixysoft.DesignPattern;
using Pixysoft.Framework.Remoting.Core;

namespace Pixysoft.Framework.Remoting
{
    public class CacheRemotingManager : Singleton<CacheRemotingManager>
    {
        public static void LoadCache()
        {
            CacheController.Load();
        }

        public static void SaveCache()
        {
            CacheController.Save();
        }

        public static T[] GetMethodResponses<T>(string methodName)
        {
            List<T> responses = new List<T>();

            foreach (string rawresponse in CacheController.Instance.GetCacheMethodResponses(methodName))
            {
                responses.Add((T)ReflectionController.Instance.GetDeserializeRemoteCallResult(typeof(T), rawresponse));
            }

            return responses.ToArray();
        }

        public static bool RemoveMethodResponseAt<T>(string methodName, int index)
        {
            return CacheController.Instance.RemoteMethodResponseAt(methodName, index);
        }

        public IRemotingCacheSetting Setting
        {
            get
            {
                return SettingHelper.Configuration.Get(this.SingletonId);
            }
        }

        public IRemoteChannel<T> CreateClearChannel<T>()
        {
            return new CacheClearRemoteChannel<T>();
        }

        public IRemoteChannel<T> CreateRemoteChannel<T>(string url)
        {
            return CreateRemoteChannel<T>(url, null);
        }

        public IRemoteChannel<T> CreateRemoteChannel<T>(string url, string token)
        {
            return CreateRemoteChannel<T>(url, null, Setting);
        }

        public IRemoteChannel<T> CreateRemoteChannel<T>(string url, string token, IRemotingCacheSetting setting)
        {
            return new CacheRemoteChannel<T>(url, token, setting);
        }
    }
}
